from pathlib import Path
from core.backup import BackupManager
from plugins.template.adapter import PluginBase

class GentooAdapter(PluginBase):
    def __init__(self):
        self.conf_path = Path("/etc/portage/make.conf")
        self.backup = BackupManager("gentoo")
    
    @classmethod
    def is_compatible(cls) -> bool:
        return GentooDetector().detect()
    
    def apply_mirror(self, mirror_url: str):
        self.backup.create(self.conf_path)
        content = self.conf_path.read_text()
        
        # 替换或添加 SYNC 配置
        if "SYNC=" in content:
            new_content = re.sub(r'SYNC=".*"', f'SYNC="{mirror_url}/gentoo-portage"', content)
        else:
            new_content = content + f'\nSYNC="{mirror_url}/gentoo-portage"'
        
        self.conf_path.write_text(new_content)